package com.uhu.sys.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uhu.sys.dto.req.ClientListReqDTO;
import com.uhu.sys.dto.res.ClientListResDTO;
import com.uhu.sys.entity.Client;
import com.uhu.sys.dao.ClientMapper;
import com.uhu.sys.entity.Staff;
import com.uhu.sys.service.IClientService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 客户表 服务实现类
 * </p>
 *
 * @author liujunjie
 * @since 2024-01-04
 */
@Service
public class ClientServiceImpl extends ServiceImpl<ClientMapper, Client> implements IClientService {

    @Autowired
    private ClientMapper clientMapper;

    @Override
    public Page<List<ClientListResDTO>> getClientPageList(ClientListReqDTO clientListReqDTO) {
        Page page = new Page();
        page.setCurrent(clientListReqDTO.getPageNumber());
        page.setSize(clientListReqDTO.pageSize);
        if (!clientListReqDTO.getPageNumber().equals(1L)) {
            // 非一页 不取count
            page.setSearchCount(false);
        }
        QueryWrapper qw = new QueryWrapper<Staff>()
                .eq(ObjectUtil.isNotNull(clientListReqDTO.getClientId()), "client_id", clientListReqDTO.getClientId())
                .eq(StrUtil.isNotBlank(clientListReqDTO.getClientCode()), "client_code", clientListReqDTO.getClientCode())
                .eq(ObjectUtil.isNotNull(clientListReqDTO.getClientSex()), "client_sex", clientListReqDTO.getClientSex())
                .eq(ObjectUtil.isNotNull(clientListReqDTO.getClientStatus()), "client_status", clientListReqDTO.getClientStatus())
                .orderByAsc("client_id");
        return clientMapper.selectPage(page, qw);
    }
}
